﻿using Domain.Interface.Organization;
using Framework.Domain.DomainObject.Bo;
using Framework.Domain.Interface.Factorys;
using Repository.Interface.Organization;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Text;
using System.Linq;

namespace Domain.Organization.BusinessObjects
{
    [Table("Departments")]
    public class DepartmentEntity : BaseBoEntity
    {
        /// <summary>
        /// 部门名
        /// </summary>
        public string DepartmentName { get; set; }

        /// <summary>
        /// 部门编号
        /// </summary>
        public string DepartmentNo { get; set; }

        /// <summary>
        /// 父级KeyId
        /// </summary>  
        public Guid? ParentKeyId { get; set; }

        /// <summary>
        /// 部门负责人KeyId
        /// </summary>  
        public Guid? DeptChiefKeyId { get; set; }

        /// <summary>
        /// 部门级别
        /// </summary> 
        public int Level { get; set; }

        /// <summary>
        /// 地址
        /// </summary>   
        public string Address { get; set; }

    }

    public class DepartmentDomain : BaseBo, IDepartmentDomain
    {
        /// <summary>
        /// 数据实体
        /// </summary>
        private DepartmentEntity SelfEntity
        {
            get { return this.Entity as DepartmentEntity; }
            set { this.Entity = value; }
        }

        #region 构造函数
        public DepartmentDomain(DepartmentEntity entity)
        {
            this.SelfEntity = entity;
        }

        public DepartmentDomain(string name)
        {
            this.SelfEntity = new DepartmentEntity()
            {
                KeyId = Guid.NewGuid(),
                DepartmentName = name,
                CreateTime = DateTime.Now,
                UpdateTime = DateTime.Now,
                IsDelete = false,
                Version = 1,
                CreateUserKeyId = Guid.Parse("99999999-9999-9999-9999-999999999999"),
                UpdateUserKeyId = Guid.Parse("99999999-9999-9999-9999-999999999999"),
            };
        }
        #endregion

        #region Domain 属性

        #region Set

        /// <summary>
        /// 部门名
        /// </summary>
        public void SetDepartmentName(string DepartmentName)
        {
            this.SelfEntity.DepartmentName = DepartmentName;
        }

        /// <summary>
        /// 部门编号
        /// </summary>
        public void SetDepartmentNo(string DepartmentNo)
        {
            this.SelfEntity.DepartmentNo = DepartmentNo;
        }

        /// <summary>
        /// 父级KeyId
        /// </summary>
        public void SetParentKeyId(Guid? ParentKeyId)
        {
            this.SelfEntity.ParentKeyId = ParentKeyId;
        }

        /// <summary>
        /// 部门负责人KeyId
        /// </summary>
        public void SetDeptChiefKeyId(Guid? DeptChiefKeyId)
        {
            this.SelfEntity.DeptChiefKeyId = DeptChiefKeyId;
        }

        /// <summary>
        /// 部门级别
        /// </summary>
        public void SetLevel(int Level)
        {
            this.SelfEntity.Level = Level;
        }

        /// <summary>
        /// 地址
        /// </summary>
        public void SetAddress(string Address)
        {
            this.SelfEntity.Address = Address;
        }


        #endregion

        #region Get
        /// <summary>
        /// 部门名
        /// </summary>
        public string GetDepartmentName()
        {
            return this.SelfEntity.DepartmentName;
        }

        /// <summary>
        /// 部门编号
        /// </summary>
        public string GetDepartmentNo()
        {
            return this.SelfEntity.DepartmentNo;
        }

        /// <summary>
        /// 父级KeyId
        /// </summary>
        public Guid? GetParentKeyId()
        {
            return this.SelfEntity.ParentKeyId;
        }

        /// <summary>
        /// 部门负责人KeyId
        /// </summary>
        public Guid? GetDeptChiefKeyId()
        {
            return this.SelfEntity.DeptChiefKeyId;
        }

        /// <summary>
        /// 部门级别
        /// </summary>
        public int GetLevel()
        {
            return this.SelfEntity.Level;
        }

        /// <summary>
        /// 地址
        /// </summary>
        public string GetAddress()
        {
            return this.SelfEntity.Address;
        }

        #endregion 

        #endregion

        /// <summary>
        /// 获取当前部门所有的子部门
        /// </summary>
        /// <returns></returns>
        public List<IDepartmentDomain> FindCurrentDeptSubDeptList()
        {
            IDepartmentRepository deptRepository = ExampleFactory.CreateAppService<IDepartmentRepository>();
            return deptRepository.GetQuery(new Repository.Interface.SearchCriterias.DepartmentSearchCriteria()
            {
                ParentKeyId = this.SelfEntity.KeyId
            }).ToList();
        }




    }



}
